import {
    AfterContentChecked, AfterViewChecked, AfterViewInit, Component, ElementRef, Input, OnChanges, OnDestroy, OnInit,
    ViewChild
} from '@angular/core';
import {ServerSocket} from "@core/services/ws.service";

@Component({
  selector: 'app-c-tieba',
  templateUrl: './c-tieba.component.html',
  styleUrls: ['./c-tieba.component.less']
})
export class CTiebaComponent implements OnInit, AfterViewChecked, AfterViewInit, OnDestroy, OnChanges, AfterContentChecked {

    @Input() data;
    @Input() socket;
    @Input() IPPORT;
    @ViewChild('answer') box: ElementRef;
    @ViewChild('list') list: ElementRef;
    constructor(public ws: ServerSocket) { }

    ngOnInit() {

    }

    _answerList = [];
    // 正式时为父组件socket接收数据 @Input()至此组件;
    @Input()
    set answerList(value) {
        this._answerList = value;
        this.box.nativeElement.scrollTop = this.list.nativeElement.offsetHeight - this.box.nativeElement.offsetHeight;
        console.log(this.box.nativeElement.scrollTop);
    }

    get answerList() {
        return this._answerList;
    }
    // 测试
    // time;
    ngAfterViewInit() {
        // this.time = setInterval(() => {
        //     this._answerList.push({
        //         UserName: '学生名',
        //         answer: '评论内容',
        //         img: '',
        //         createtime: '2018-01-01 20:00:00'
        //     });
        //     this.box.nativeElement.scrollTop = this.list.nativeElement.offsetHeight - this.box.nativeElement.offsetHeight;
        // }, 5000);
    }

    ngAfterContentChecked() {
    }

    ngAfterViewChecked() {
        this.box.nativeElement.scrollTop = this.list.nativeElement.offsetHeight - this.box.nativeElement.offsetHeight;
    }


    ngOnDestroy() {
        // clearInterval(this.time);
    }

    ngOnChanges() {
        this.box.nativeElement.scrollTop = this.list.nativeElement.offsetHeight - this.box.nativeElement.offsetHeight;
    }

}
